package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.sky.exception.ServiceException;
import org.sky.quartz.QuartzManager;
import org.sky.sys.entity.SysQuartz;
import org.sky.sys.service.SysQuartzService;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* @author weifx
* @Title: SysQuartzController
* @ProjectName platform-core
* @Description: TODO{计划任务服务类}
* @date 2019-3-14 21:50:03
* @Version 1.0
*/
@Controller
@RequestMapping(value="/sys/SysQuartz")
public class SysQuartzController {
    @Autowired
    private SysQuartzService sysQuartzService;
    @Autowired
    private QuartzManager quartzManager;
    /**
    * 打开计划任务页面
    * @return
    */
    @GetMapping(value="/initSysQuartzListPage")
    public String initSysQuartzListPage(){
        return "sys/quartz/sysquartzlist";
    }
    /**
    * 计划任务分页查询
    * @param request
    * @return
    */
    @PostMapping(value = "/selectSysQuartzByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectSysQuartzByPage(HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysQuartz.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysQuartzService.selectSysQuartzByPage(pageNum,pageSize,example);
        //处理状态
        for(Object obj:pageData.getRows()){
            SysQuartz quartz = (SysQuartz)obj;
            quartz.setStatus(quartzManager.getTriggerState(quartz));
        }
        return JsonUtils.obj2json(pageData);
    }
    /**
    * 打开计划任务新增页面
    * @return
    */
    @GetMapping(value="/initSysQuartzAddPage")
    public String initSysQuartzAddPage(){
        return "sys/quartz/sysquartzedit";
    }
    /**
    * 打开计划任务编辑页面
    * @return
    */
    @GetMapping(value="/initSysQuartzEditPage")
    public String initSysQuartzEditPage(){
        return "sys/quartz/sysquartzedit";
    }
    /**
    * 打开计划任务明细页面
    * @return
    */
    @GetMapping(value="/initSysQuartzDetailPage")
    public String initSysQuartzDetailPage(){
        return "sys/quartz/sysquartzdetail";
    }
    /**
    * 保存{添加/修改}计划任务
    * @param request
    * @return
    */
    @PostMapping(value = "/saveAddEditSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysQuartz(HttpServletRequest request){
        Result result = null;
        try {
            SysQuartz sysQuartz = RequestUtils.getEntityBean(request, SysQuartz.class);
            sysQuartzService.saveAddEditSysQuartz(sysQuartz);
            result = Result.success("保存成功");
        }catch (ServiceException e){
            result = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
    * 根据Id查询计划任务
    * @param id
    * @return
    */
    @GetMapping(value = "/getSysQuartzById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysQuartzById(
        @PathVariable Long id){
        SysQuartz bean = sysQuartzService.getSysQuartzById(id);
        return JsonUtils.obj2json(bean);
    }
    /**
    * 根据ID删除计划任务
    * @param request
    * @return
    */
    @PostMapping(value = "/delSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysQuartz(HttpServletRequest request){
        Result result= null;
        try {
            String delRows=request.getParameter("delRows");
            List<SysQuartz> de=JsonUtils.json2list(delRows, SysQuartz.class);
            sysQuartzService.delSysQuartzById(de);
            result = Result.success("删除成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 启动定时任务
     * @param request
     * @return
     */
    @PostMapping(value = "/runSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String runSysQuartz(
            HttpServletRequest request){
        Result result = null;
        try {
            String jobStr=request.getParameter("job");
            List<SysQuartz> list=JsonUtils.json2list(jobStr, SysQuartz.class);
            //先移除　再启动
            for(SysQuartz job:list){
                quartzManager.runJob(job);
            }
            result = Result.success("启动成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("启动失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 停止定时任务
     * @param request
     * @return
     */
    @PostMapping(value = "/stopSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String stopSysQuartz(
            HttpServletRequest request){
        Result result = null;
        try {
            String jobStr=request.getParameter("job");
            List<SysQuartz> list=JsonUtils.json2list(jobStr, SysQuartz.class);
            //先移除　再启动
            for(SysQuartz job:list){
                quartzManager.delJob(job);
            }
            result = Result.success("停止成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("停止失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 暂停定时任务
     * @param request
     * @return
     */
    @PostMapping(value = "/pauseSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String pauseSysQuartz(
            HttpServletRequest request){
        Result result = null;
        try {
            String jobStr=request.getParameter("job");
            List<SysQuartz> list=JsonUtils.json2list(jobStr, SysQuartz.class);
            for(SysQuartz job:list){
                quartzManager.pauseJob(job);
            }
            result = Result.success("暂停成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("暂停失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 恢复定时任务
     * @param request
     * @return
     */
    @PostMapping(value = "/resumeSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String resumeSysQuartz(
            HttpServletRequest request){
        Result result = null;
        try {
            String jobStr=request.getParameter("job");
            List<SysQuartz> list=JsonUtils.json2list(jobStr, SysQuartz.class);
            for(SysQuartz job:list){
                quartzManager.resumeJob(job);
            }
            result = Result.success("恢复成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("恢复失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 立即执行定时任务
     * @param request
     * @return
     */
    @PostMapping(value = "/triggerSysQuartz",produces = "application/json;charset=UTF-8")
    public @ResponseBody String triggerSysQuartz(
            HttpServletRequest request){
        Result result = null;
        try {
            String jobStr=request.getParameter("job");
            List<SysQuartz> list=JsonUtils.json2list(jobStr, SysQuartz.class);
            for(SysQuartz job:list){
                quartzManager.triggerJob(job);
            }
            result = Result.success("执行成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("执行失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
}